<?php
/**
* @author: trungdt - May31, 2011
*/

class rsnewsmodule_config {
	function form($object) {
		$i18n = exponent_lang_loadFile('datatypes/rsnewsmodule_config.php');

		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();

		global $db;
		$form = new form();
		if (!isset($object->id)) {
			$object->sortorder = 'DESC';
			$object->sortfield = 'posted';
			$object->item_limit = 10;
			$object->aggregate = array();
			$object->image_group = 0;
		} else {
            
		}

		// setup the listbuilder arrays for news aggregation.
                $loc = unserialize($object->location_data);
                $news = exponent_modules_getModuleInstancesByType('rsnewsmodule');
                $saved_aggregates = empty($object->aggregate) ? array() : unserialize($object->aggregate);
                $all_news = array();
                $selected_news = array();
                foreach ($news as $src => $cal) {
                        $news_name = (empty($cal[0]->title) ? 'Untitled' : $cal[0]->title).' on page '.$cal[0]->section;
                        if ($src != $loc->src) {
                                if (in_array($src, $saved_aggregates)) {
                                        $selected_news[$src] = $news_name;
                                } else {
                                        $all_news[$src] =  $news_name;
                                }
                        }
                }

		$opts  = array('ASC'=>$i18n['ascending'],'DESC'=>$i18n['descending']);
		$fields = array('posted'=>$i18n['posteddate'],'publish'=>$i18n['publishdate'],'edited'=>'Date of Last Edit');
		$form->register(null,'',new htmlcontrol('<h1>'.$i18n['general_conf'].'</h1><hr size="1" />'));
		
		$form->register('image_group',"News Image group",new file_collection_control($object->image_group));
		
		$form->register('item_limit',$i18n['item_limit'],new textcontrol($object->item_limit));
		$form->register('sortorder',$i18n['sortorder'], new dropdowncontrol($object->sortorder,$opts));
		$form->register('sortfield',$i18n['sortfield'], new dropdowncontrol($object->sortfield,$fields));

	 	$form->register(null,'',new htmlcontrol('<h1>'.$i18n['merge_news'].'</h1><hr size="1" />'));
        $form->register('aggregate',$i18n['pull_news'],new listbuildercontrol($selected_news,$all_news));

		$form->register('submit','', new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));

		return $form;
	}

	function update($values,$object) {
		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
            exponent_forms_initialize();
		$object->sortorder = $values['sortorder'];
		$object->sortfield = $values['sortfield'];
		
		$object->image_group = intval($values['image_group']);
		
		$object->aggregate = serialize(listbuildercontrol::parseData($values,'aggregate'));
		
		if ($values['item_limit'] > 0) {
			$object->item_limit = intval($values['item_limit']);
		} else {
			$object->item_limit = 10;
		}

		return $object;
	}
}

?>
